package org.vector.serviceImpl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.vector.service.BankService;
import org.vector.utils.BaseDao;
import org.vector.view.Wcuowu;

public class BankServiceImpl implements BankService {
	BaseDao bd = new BaseDao();
	/**
	 * 存钱
	 */
	@Override
	public int save(int password, int money,int bankid) {
		String sql = "update bank set money = money+? where bankid = ? and ccdpassword = ?";
		int rows = bd.executeUpdate(sql, money,bankid,password);
		return rows;
	}
	/**
	 * 转账
	 */
	@Override
	public int transfer(int password, int money, int bankid,int bankid2) {
		String sql1 = "select money from bank where bankid = ?";
		ResultSet res = bd.executeQuery(sql1, bankid);
		try {
			if(res.next()) {
				int money2 = res.getInt("money");
				if(money2 > money) {
					String sql = "update bank set money = money-? where bankid = ? and ccdpassword = ?";
					int rows = bd.executeUpdate(sql, money,bankid,password);
					if(rows == 1) {
						String sql2 = "update bank set money = money+? where bankid = ? ";
						int rows2 = bd.executeUpdate(sql2, money-3,bankid2);
						return rows2;
					}else {
						return 0;
					}
				}else {
					new Wcuowu("钱不够，你只有"+ money2);
					return 0;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	/**
	 * 取钱
	 */
	@Override
	public int draw(int password, int money,int bankid) {
		String sql1 = "select money from bank where bankid = ?";
		ResultSet res = bd.executeQuery(sql1, bankid);
		try {
			if(res.next()) {
				int money2 = res.getInt("money");
				if(money2 > money) {
					String sql = "update bank set money = money-? where bankid = ? and ccdpassword = ?";
					int rows = bd.executeUpdate(sql, money,bankid,password);
					return rows;
//					if(rows == 1) {
//						
//						new Wcuowu("成功");
//					}else {
//						new Wcuowu("密码错误");
//					}
				}else {
					new Wcuowu("钱不够，你只有"+ money2);
					return 0;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
		
		
	}
	/**
	 * 记录
	 */
	@SuppressWarnings("unused")
	@Override
	public String[][] getRecord(int bankid) {
		int num = 0;
		String sql = "select gouwuid,bankid,bankid2,themoney,xiaofei,sserivice,datatime from daytodaycard where bankid = ?";
		//String sql = "select * from daytodaycard";
		ResultSet resultset = bd.executeQuery(sql,bankid);
		try {
			while(resultset.next()) {
				num ++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][7];
		ResultSet r = bd.executeQuery(sql,bankid);
		int row = 0;
		try {
			while(r.next()) {
				int gouwuid = r.getInt("gouwuid");
				int bankidd = r.getInt("bankid");
				int bankid2 = r.getInt("bankid2");
				int themoney = r.getInt("themoney");
				int xiaofei = r.getInt("xiaofei");
				String sserivice = r.getString("sserivice");
				String datatime = r.getString("datatime");
				
				String gouwuidc = Integer.toString(gouwuid);
				String bankiddc = Integer.toString(bankidd);
				String bankid2c = Integer.toString(bankid2);
				String themoneyc = Integer.toString(themoney);
				String xiaofeic = Integer.toString(xiaofei);
				
				data[row][0]=gouwuidc;
				data[row][1]=bankiddc;
				data[row][2]=bankid2c;
				data[row][3]=themoneyc;
				data[row][4]=xiaofeic;
				data[row][5]=sserivice;
				data[row][6]=datatime;
				
				row ++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}
	/**
	 * 
	 */
	@Override
	public void savedaytoday(int gouwuid, int bankid, int bankid2, int money, int xiaofei, String sserivice,
			String datatime) {
		String sql = "select gouwuid from daytodaycard where gouwuid = ?";
		ResultSet resultset = bd.executeQuery(sql,gouwuid);
		try {
			if(resultset.next()) {
				new Wcuowu("，流水单号已经存在，请重新操作，");
			}else {
				String sql2 = "insert into daytodaycard values(?,?,?,?,?,?,?)";
				int rows = bd.executeUpdate(sql2,gouwuid,bankid,bankid2,money, xiaofei,sserivice,datatime);
				if(rows == 1) {
					new Wcuowu("交易已被记录");
				}else {
					new Wcuowu("交易出现异常，为保存成功");
				}
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		
		
	}
	
}
